package cn.edu.neusoft.cqy917.zyfypt_cqy917.model;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.CollectListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.Collectiface;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.service.CollectService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;



public class CollectModel implements Collectiface{
    private CollectService collectService;
    private Retrofit retrofit;
    private static final  String BASE_URL=
            "http://amicool.neusoft.edu.cn/";
    public CollectModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(
                        ScalarsConverterFactory.create())
                .build();

    }

    @Override
    public void collect(String mod, int resid, String sessionID, final CollectListener collectListener) {
        collectService =retrofit.create(CollectService.class);
        Call<String> call =collectService.collect(mod,resid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    collectListener.oncollectResponse(response.body());
                else
                    collectListener.oncollectFail("error");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                collectListener.oncollectFail("error");
            }
        });
    }

    @Override
    public void uncollect(String mod, int resid, String sessionID,final CollectListener collectListener) {
        collectService =retrofit.create(CollectService.class);
        Call<String> call =collectService.uncollect(mod,resid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    collectListener.oncancelResponse(response.body());
                else
                    collectListener.oncancelFail("error");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                collectListener.oncancelFail("error");
            }
        });
    }

    @Override
    public void judge(String mod, int resid, String sessionID, final CollectListener collectListener) {
        collectService =retrofit.create(CollectService.class);
        Call<String> call =collectService.judge(mod,resid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    collectListener.onjudgeResponse(response.body());
                else
                    collectListener.onjudgeFail("error");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                collectListener.onjudgeFail("error");
            }
        });
    }

}
